package org.millioncall.yueyoga.admin.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.millioncall.openplatform.util.StringUtils;
import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.model.Reservation;
import org.millioncall.yueyoga.admin.service.ReservationService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;

@Controller
public class ReservationAction extends BaseAction {

	private ReservationService reservationService;

	public ReservationService getReservationService() {
		return reservationService;
	}

	public void setReservationService(ReservationService reservationService) {
		this.reservationService = reservationService;
	}

	/** 查询订座列表-哪一天，哪个课程预定座位 */
	@RequestMapping(value = "/ajaxGetReservations.jhtml")
	public void ajaxReservations(String curDate, String ydId, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		String msg = "";
		if (StringUtils.isBlank(ydId) || StringUtils.isBlank(curDate)) {
			msg = "{success:false,items:'查询条件缺失!'}";
		} else {
			Map<String, Object> maps = new HashMap<String, Object>();
			maps.put("cheduleTime", curDate);
			maps.put("ydId", ydId);
			List<Reservation> reList = reservationService.findReservationByCdts(maps);
			String items = JSONArray.toJSONString(reList);
			msg = "{success:true,items:" + items + "}";
		}
		WebAppUtils.ajaxResponse(response, msg);
	}
	
	@RequestMapping(value="/ajaxCancelReserve.jhtml")
	public void ajaxCancelReserve(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		String msg = "";
		String strId = request.getParameter("scheduleId");
		if (StringUtils.isBlank(strId)) {
			msg = "{'success':false, 'msg':'序号不能为空！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		int scheduleId = 0;
		try {
			scheduleId = Integer.parseInt(strId);
		} catch (NumberFormatException e) {
			msg = "{'success':false, 'msg':'序号格式不正确！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("scheduleId", scheduleId);
		int i = this.reservationService.cancelReserve(map);
		if (i > 0) {
			msg = "{'success':true, 'msg':'取消成功！'}";
		} else {
			msg = "{'success':false, 'msg':'取消失败！'}";
		}
		
		WebAppUtils.ajaxResponse(response, msg);
	}

	/** 会员预定座位 */
	@RequestMapping(value = "/ajaxMemberReservation.jhtml")
	public void ajaxMemberReservation(String ydId, String memberId, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		String msg = "";
		if (StringUtils.isBlank(ydId) || StringUtils.isBlank(memberId)) {
			msg = "{success:false,msg:'预定座位失败!'}";
		} else {
			if (reservationService.reservateSingleSeat(ydId,memberId)) {
				msg = "{success:true,msg:'预定座位成功!'}";
			}
		}
		WebAppUtils.ajaxResponse(response, msg);
	}
	
	@RequestMapping(value="/ajaxCancelReservation.jhtml")
	public void ajaxCancelReservation(String reservateId, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		String msg = "";
		if (StringUtils.isBlank(reservateId) || StringUtils.isBlank(reservateId)) {
			msg = "{success:false,msg:'取消预定座位失败!'}";
		} else {
			if (reservationService.cancelReservation(reservateId)) {
				msg = "{success:true,msg:'取消预定座位成功!'}";
			}
		}
		WebAppUtils.ajaxResponse(response, msg);
	}
	
}
